/*
 * Copyright (c) 2022 LookerSong
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import prompt from '@system.prompt';
import Poker from "../../common/Poker.js";

let first, second = 0;      // 第一个数、第二个数下标

export default {
    data: {
        score: 0,       // 得分
        operdis: true,      // 运算符不可点击
        operindex: null,       // 运算符下标
        pokers: Poker,      // 源牌组
        origin: [],     // 初始牌组
        current: [      // 当前牌组
            {
                num: 1,     // 计算值
                text: '1',      // 左上角文本
                shape: "common/images/spade.png",      // 花型
                show: true,     // 显示标识符
                disabled: false,        // 不可交互属性
                css: "",        // 样式
            },
            {
                num: 11,
                text: 'J',
                shape: "common/images/heart.png",
                show: true,
                disabled: false,
                css: "",
            },
            {
                num: 12,
                text: 'Q',
                shape: "common/images/club.png",
                show: true,
                disabled: false,
                css: "",
            },
            {
                num: 13,
                text: 'K',
                shape: "common/images/diamond.png",
                show: true,
                disabled: false,
                css: "",
            }
        ],
        operator: [     // 加、减、乘、除运算符图片及选中样式
            {
                src: "common/images/add.png",
                css: "",
            },
            {
                src: "common/images/sub.png",
                css: "",
            },
            {
                src: "common/images/mult.png",
                css: "",
            },
            {
                src: "common/images/div.png",
                css: "",
            },
        ],
    },

    onInit() {
        this.replace();
    },

    // 换牌方法一：将4*13共52张牌先乱序后，取前4张
    disorder() {
        let many, ran, temp = 0;
        for(many=0; many<26; many++) {
            ran = Math.floor(Math.random()*52);
            temp = this.pokers[many];
            this.pokers[many] = this.pokers[ran];
            this.pokers[ran] = temp;
        }
        this.origin = [0, 1, 2, 3];
    },

    // 换牌方法二：52张牌排序不变，随机生成4个不重复的数，按数组下标取4张牌
    getrandom() {
        this.origin = [];
        let compare = false;
        let temp = 0;
        while(4 > this.origin.length) {
            compare = false;
            temp = Math.floor(Math.random()*52);
            for(let i=0; i<this.origin.length; i++) {
                if(temp == this.origin[i]) {
                    // 抽到两张相同的牌，不放入初始牌组
                    compare = true;
                    break;
                }
            }
            if(false == compare) {
                this.origin.push(temp);
            }
        }
    },

    // 牌组赋值
    initcards() {
        for(let i=0; i<4; i++) {
            this.current[i].num = this.pokers[this.origin[i]].num;
            this.current[i].text = this.pokers[this.origin[i]].text;
            this.current[i].shape = this.pokers[this.origin[i]].shape;
            this.current[i].show = true;
            this.current[i].disabled = false;
            this.current[i].css = "";
        }
        this.operdis = true;
        if(null != this.operindex) {
            this.operator[this.operindex].css = "";
            this.operindex = null;
        }
    },

    // 换一组牌，并赋值给当前牌组
    replace() {
        console.log("—————————更换牌组—————————");
//        this.disorder();        // 换牌方法一
        this.getrandom();       // 换牌方法二
        this.initcards();
        console.log("当前牌组为" + JSON.stringify(this.current));
    },

    // 还原牌组
    revert() {
        console.log("—————————还原牌组—————————");
        this.initcards();
        console.log("还原当前牌组为" + JSON.stringify(this.current));
    },

    // 选择数字
    optnum(num_index) {
        // 运算符为空，处于选第一个数的环节
        if(null == this.operindex) {
            this.operdis = false;
            first = num_index;
            for(let i=0; i<4; i++) {
                this.current[i].css = "";
            }
            this.current[num_index].css = "select";
        }
        // 运算符不为空，选择了第二个数
        else {
            this.operdis = true;
            second = num_index;
            this.calculate();
        }
    },

    // 选择运算符
    setsign(op_index) {
        // 若点击的运算符为未选中状态，
        if("" == this.operator[op_index].css) {
            for(let i=0; i<4; i++) {
                this.operator[i].css = "";
            }
            this.operator[op_index].css = "select";
            this.operindex = op_index;

            // 关闭第一个数可选中，取消其同时作为第二个数的资格
            this.current[first].disabled = true;
        }
        // 若点击的运算符为选中状态，取消选中，重启选第一个数环节
        else {
            this.operator[op_index].css = "";
            this.current[first].disabled = false;
            this.operindex = null;
        }
    },

    // 判别运算是否合法，再进行运算
    calculate() {
        // 选好第二个数，除法运算是否合法
        if((3 == this.operindex) && (0 == this.current[second].num)) {
            prompt.showToast({
                message: "0不能作为除数，请重新选择",
                duration: 1500,
            });
            // 非法运算，重置选数环节
            this.current[first].disabled = false;
            this.current[first].css = "";
            this.operator[this.operindex].css = "";
            this.operindex = null;
            this.operdis = true;
            first = second = 0;
        }
        else if((3 == this.operindex) && (0 != (this.current[first].num % this.current[second].num))) {
            prompt.showToast({
                message: "无法被整除，请重新选择",
                duration: 1500,
            });
            // 无法被整除，重置选数环节
            this.current[first].disabled = false;
            this.current[first].css = "";
            this.operator[this.operindex].css = "";
            this.operindex = null;
            this.operdis = true;
            first = second = 0;
        }
        // 正常触发运算
        else {
            switch (this.operindex) {
                case 0:
                    this.current[first].num += this.current[second].num;
                    break;
                case 1:
                    this.current[first].num -= this.current[second].num;
                    break;
                case 2:
                    this.current[first].num *= this.current[second].num;
                    break;
                case 3:
                    this.current[first].num /= this.current[second].num;
                    break;
                default:
                    console.log("运算意外出错");
                    break;
            }
            // 运算结束，将结果赋值给一，文本显示数字，花型为某种，恢复一的可点击和未选中样式，隐藏二的显示，运算符为null
            this.current[first].text = this.current[first].num;
            this.current[first].shape = "";
            this.current[first].disabled = false;
            this.current[first].css = "";
            this.current[second].show = false;
            this.operator[this.operindex].css = "";
            this.operindex = null;
            this.checkover();
        }
    },

    // 得分判定，先判断是否只剩一张牌，若是则进行结果比较，答对加分换牌，答错还原
    checkover() {
        let temp = 4;
        for(let i=0; i<4; i++) {
            if(false == this.current[i].show) {
                temp --;
            }
        }
        if(1 == temp) {
            // 结果判断是否等于24
            if(24 == this.current[first].num) {
                prompt.showToast({
                    message: "答对啦，加分，换牌组",
                    duration: 1500,
                });
                this.score += 10;
                this.replace();
            }
            else {
                prompt.showToast({
                    message: "答错啦……还原牌组",
                    duration: 1500,
                });
                this.revert();
            }
        }
    },
}
